package org.come.thread;

import java.util.*;

import com.tool.ModerateTask.*;
import com.tool.imagemonitor.*;
import com.tool.time.Limit;
import com.tool.time.TimerUtil;
import org.come.Frame.*;
import org.come.Jpanel.ActivityModelJpanel;
import org.come.Jpanel.ZhuJpanel;
import org.come.action.MapAction;
import org.come.action.NpcMenuAction;
import org.come.bean.AllActive;
import org.come.bean.LoginResult;
import org.come.bean.NpcInfoBean;
import org.come.bean.RoleShow;
import org.come.control.JirenjiaobenchuliControl;
import org.come.entity.Goodstable;
import org.come.model.ActiveBase;
import org.come.model.Door;
import org.come.mouslisten.Mouselistener;
import org.come.npc.TP;
import org.come.socket.Agreement;
import org.come.socket.GameClient;
import org.come.socket.SendMessageUntil;
import org.come.until.BabyUntil;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.MessagrFlagUntil;
import org.come.until.NpcMenuUntil;
import org.come.until.UserMessUntil;
import org.come.until.Util;
import com.tool.PlayerKill.PKSys;
import com.tool.image.ImageMixDeal;
import com.tool.image.ManimgAttribute;
import com.tool.role.RoleData;
import com.tool.tcp.SpriteFactory;
import com.tool.time.TimeLimit;
import come.tool.Fighting.FightingMixDeal;
import come.tool.map.XLPath;

public class TimeControlRunnable implements Runnable {
    private int jg=0;
    private int tx=0;
    public static int JBTime=0;//防脚本
    private static boolean isScript=false;
    private static Object object=new Object();
    public TimeControlRunnable() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (true) {
            try {
                long x=System.currentTimeMillis();
                int cha=25;
                if (FightingMixDeal.State==0){
                    tx=6;
                    cha=150;
                }else {
                    tx++;
                    FightingMixDeal.PalyProgress(25);
                }
                if (tx>5) {
                    tx=0;
                    CommonUse();
                }
                x=System.currentTimeMillis()-x;
                if (x<cha){
                    Thread.sleep(cha-x);
                }
                Util.addtime(cha);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    /**通用处理*/
    public void CommonUse(){
        jg++;
        if (JBTime>0) {
            JBTime-=150;
            if (JBTime<=0) {
                AntiPluginJframe.getAntiPluginJframe().getPluginJpanel().overtime();
            }
        }
        // 画出组队申请
        if (ZhuFrame.getZhuJpanel().getTeamState() == 1){
            ZhuJpanel.getGroupBtn().imgchange((ZhuJpanel.getGroupBtn().getImgzhen() + 1) % 2);
        }
        // 画出好友消息friends
        if (MessagrFlagUntil.NotReads.size() > 0){
            ZhuJpanel.getFriendsBtn().imgchange((ZhuJpanel.getFriendsBtn().getImgzhen() + 1) % 2);
        }
        if (isScript&&jg%4==0) {
            try {
                scriptHandle();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
        if (jg%30==0) {
            TimeLimit.Timeout();
        }else if (jg>500) {
            jg=0;
            // 判断宝宝状态
            if (RoleData.getRoleData().getLoginResult().getHavebaby() > 0){
                BabyUntil.calculateBaby();
            }
            Hero.getHero().OverTime();
            PKSys.getPkSys().isexpiation();
            SpriteFactory.ResetAndRemove();
            CutButtonImage.removeNPCHeadImg();
            for (int i = ImageMixDeal.npcimglist.size()-1; i >=0 ; i--) {
                ImageMixDeal.npcimglist.get(i).npcmsg();
            }
            GoodsListFromServerUntil.isSendPackRecord();
        }
    }
    /**脚本处理*/
    public void scriptHandle(){
        synchronized(TimeControlRunnable.object) {
            try {
                if (ImageMixDeal.userimg.getRoleShow().getFighting()!=0){
                    return;
                }
                int size = script.size();
                //  System.out.println(size);
                if (size == 0) {
                    TP.isfly=false;
                    isScript = false;
                } else {
                    --size;
                    Object object = script.get(size);
                    if (object == null) {
                        script.remove(size);
                    } else {
                        if (object instanceof ScriptOpen) {
                            ScriptOpen open = (ScriptOpen)object;
                            if (open.getType() == 0) {
                                Mouselistener.XL(open.getX() / 20, open.getY() / 20);
                            } else {
                                ManimgAttribute attribute;
                                if (open.getType() == 1) {
                                    attribute = ImageMixDeal.getNpc(String.valueOf(open.getNpc()));
                                    if (attribute != null) {
                                        NpcMonitor.npc2(attribute);
                                    }
                                } else if (open.getType() == 2) {
                                    attribute = ImageMixDeal.getTask(open.getNpc());
                                    if (attribute != null) {
                                        TaskMonitor.TaskCreeps(attribute.getTaskdata());
                                    }
                                } else if (open.getType() == 3) {
                                    Door door = UserMessUntil.getDoor(String.valueOf(open.getDoor()));
                                    if (door != null) {
                                        TP.tp(door, 3);
                                    }

                                    FormsManagement.HideForm(27);
                                } else if (open.getType() == 101) {
                                    TP.tp(open.getDoor(), open.getX(), open.getY());
                                } else if (open.getType() == 102) {
                                    NpcInfoBean npcInfoBean=null;
                                    if (NPCJfram.getNpcJfram().getNpcjpanel().getNpcInfoBean()==null){
                                        Thread.sleep(1000*3);
                                        AllActive activeBase=UserMessUntil.getAllVipActive();
                                        for (ActiveBase activeBase1:activeBase.getBases()){
                                            if (activeBase1.getId()==open.getNpc()&&activeBase1.getGuide().split("-").length>=4){
                                                String ID=activeBase1.getGuide().split("-")[3];
                                                npcInfoBean=UserMessUntil.getnpcfromID(String.valueOf(ID));
                                                if (npcInfoBean==null){
                                                    int k=0;
                                                }
                                                break;
                                            }
                                        }
                                        if (npcInfoBean!=null)
                                            TaskMixDeal.isoption(open.getNpc(), (String)null,npcInfoBean.getNpctable().getNpcway());
                                        else {
                                            removeScript(true);
                                            ZhuFrame.getZhuJpanel().addPrompt2("自动任务脚本出错，已停止自动任务");
                                            return;
                                        }
                                    }else    TaskMixDeal.isoption(open.getNpc(), (String)null, NPCJfram.getNpcJfram().getNpcjpanel().getNpcInfoBean().getNpctable().getNpcway());

                                    FormsManagement.HideForm(27);
                                } else if (open.getType() == 103) {
                                    ((NpcMenuAction)MapAction.npcmenuAction.get(NpcMenuUntil.getMenu48())).menuControl("我是来击杀你的");
                                    FormsManagement.HideForm(27);
                                }else if (open.getType()==104) {// 野怪点击
                                    attribute=ImageMixDeal.getMonster(open.getNpc());
                                    if (attribute!=null) {
                                        CreepsMonitor.Creeps(attribute);
                                    }
                                }else if (open.getType()==105) {// 野怪击杀
                                    MapAction.npcmenuAction.get(NpcMenuUntil.getMenu48()).menuControl("攻击");
                                    FormsManagement.HideForm(27);
                                }
                            }

                            script.remove(size);
                        } else if (object instanceof ScriptEnd) {
                            ScriptEnd end = (ScriptEnd)object;
                            if (end.getType() == 0) {
                                RoleShow show = ImageMixDeal.userimg.getRoleShow();

                                if (show.getMapid().intValue() == end.getMap() && Math.abs(show.getX() - end.getX()) < 100 && Math.abs(show.getY() - end.getY()) < 100) {
                                    script.remove(size);
                                }
//                                else if (show.getFighting()==0&&show.getMapid()==end.getMap()){
//                                    if (show.getPlayer_Paths().size()<=1)
//                                    {
//                                        List<PathPoint> points = new ArrayList<>();
//                                        points.add(new PathPoint(show.getX(), show.getY()));
//                                        points.add(new PathPoint(end.getX(),end.getY() ));
//                                        Path.sendXandYToServer(points);
//
//
//                                    }
//
//                                }else if (show.getMapid()!=end.getMap()&&show.getFighting()==0){
//                                    TP.tp(end.getMap(),end.getX(),end.getY());
//                                }

                            }
                        }

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    public static boolean isVip;
    private static List<Object> script;
    private static List<ActiveBase> activeBases;//任务处理集合
    private static int count = 0;
    private static ScriptTask scriptTask;//任务处理
    public static void sortActiveBases(int id) {
        if (activeBases != null) {
            int index = -1;
            for (int i = 0; i < activeBases.size(); i++) {
                if (activeBases.get(i).getId() == id) {
                    ZhuFrame.getZhuJpanel().addPrompt2("没有找到" + activeBases.get(i).getaName());
                    index = i;
                    count++;
                    break;
                }
            }
            if (index != -1) {
                Collections.swap(activeBases, index, activeBases.size() - 1);
            }
        }
    }
    public static void addAllTask(List<ActiveBase> activeBases){
        if (activeBases != null) TimeControlRunnable.activeBases = activeBases;
        if (TimeControlRunnable.activeBases == null) return;
        if (count >= TimeControlRunnable.activeBases.size()) {
            count = 0;
            ZhuShouJframe.getZhuShouJframe().getZhuShouJpanel().getZhiXingBtn().setStart(false);
            return;
        }
        //如果不是队长 不能进行起飞操作
        RoleShow roleShow = ImageMixDeal.userimg.getRoleShow();
        if(roleShow.getTroop_id() != null && ImageMixDeal.userimg.getRoleShow().getCaptian() == 0){
            ZhuFrame.getZhuJpanel().addPrompt2("你不是队长,无法进行操作#76");
            return;
        }
        int vip = 0;
        Limit limit = TimeLimit.getLimits().getLimit("VIP");
        if (limit != null) vip = TimerUtil.residueDay(limit.getTime());

        if (vip <= 0) {
            ZhuFrame.getZhuJpanel().addPrompt2("月卡用户才可以使用超级助手");
            return;
        }
        if (TimeControlRunnable.activeBases != null) {
            for (int i = 0; i < TimeControlRunnable.activeBases.size(); i++) {
                ActiveBase activeBase = TimeControlRunnable.activeBases.get(i);
                if (activeBase.getValue() == 0) {
                    int count = TaskRoleData.SumReceive(activeBase.getSid(), 2);
                    if (count < activeBase.getNum()) {
                        String[] vls = activeBase.getGuide().split("-");
                        if (vls.length >= 4) {
                            TimeControlRunnable.addTask(new ScriptTask(vls, activeBase.getSid(), activeBase.getNum() - count), true);
                            break;
                        }
                    }
                } else if (activeBase.getValue() == 1) {
                    Task task=Hero.getHero().getSetTask(activeBase.getSid());//根据任务系列id 获取人物身上的任务
                    if (task == null) {
                        String[] vls = activeBase.getGuide().split("-");
                        List<Object> list= new ArrayList<>();
                        ScriptOpen openDoor=new ScriptOpen(101);
                        openDoor.setDoor(Integer.parseInt(vls[0]));
                        openDoor.setX(Integer.parseInt(vls[1]));openDoor.setY(Integer.parseInt(vls[2]));
                        list.add(0,openDoor);
                        ScriptOpen open=new ScriptOpen(1);
                        open.setNpc(Integer.parseInt(vls[3]));
                        list.add(0, open);

                        ScriptOpen lingQv=new ScriptOpen(102);
                        lingQv.setNpc(activeBase.getSid());
                        list.add(0, lingQv);
                        TimeControlRunnable.addScript(list);
                        break;
                    }
                    TaskProgress progress=task.getTaskProgres();
                    if (progress != null) {
                        String value = activeBase.getSkin();
                        String[] boodIds = value.split("\\|");
                        Map<Integer,String> boosIdMap = new HashMap<>();
                        for (int j = 0; j < boodIds.length; j++) {
                            String[] vs = boodIds[j].split("=");
                            boosIdMap.put(Integer.valueOf(vs[0]), vs[1]);
                        }

                        String sendmes = Agreement.getAgreement().zhuShouAgreement(activeBase.getId() + "-" + boosIdMap.get(progress.getDId()));
                        SendMessageUntil.toServer(sendmes);
                        break;
                    }
                } else {
                    int count = TaskRoleData.SumReceive(activeBase.getSid(), 2);
                    if (count < activeBase.getNum()) {
                        String sendmes = Agreement.getAgreement().zhuShouAgreement(activeBase.getId() + "-" + activeBase.getGuide());
                        SendMessageUntil.toServer(sendmes);
                        break;
                    }
                }
            }
        }
    }
    public static void addTask(ScriptTask scriptTask,int num) {
        synchronized (object) {
            removeScript();// 清理之前的脚本纪录
            TimeControlRunnable.scriptTask = null;// 清空之前的自动任务模式
            Task task = Hero.getHero().getSetTask(scriptTask.getTaskSetID());// 根据任务系列id 获取人物身上的任务
            if (task != null) {// 如果人物身上有这个任务
                TimeControlRunnable.scriptTask = scriptTask;// set 自动任务模式
                upTask(task, task.getTaskState());// 调用自动任务模式处理
            } else {// 如果人物身上没有这个任务
                // 生成脚本 前往领取任务
                RoleShow roleShow = ImageMixDeal.userimg.getRoleShow();
                List<Object> list = XLPath.ZDXL(roleShow.getX(), roleShow.getY(), roleShow.getMapid().intValue(),
                        scriptTask.getX(), scriptTask.getY(), scriptTask.getMap());
                if (list == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
                    return;
                }
                ScriptOpen open = new ScriptOpen(1);
                open.setNpc(scriptTask.getNpcID());
                list.add(0, open);

//				ScriptOpen openTwo = new ScriptOpen(102);
//				openTwo.setNpc(scriptTask.getTaskSetID());
//				list.add(0, openTwo);

                TimeControlRunnable.scriptTask = scriptTask;
                TimeControlRunnable.addScript(list);
            }
        }
    }
    public static void addTask(ScriptTask scriptTask,boolean is){
        synchronized (object) {
            TimeControlRunnable.isVip = is;
            removeScript(false);//清理之前的脚本纪录
            TimeControlRunnable.scriptTask=null;//清空之前的自动任务模式
            Task task=Hero.getHero().getSetTask(scriptTask.getTaskSetID());//根据任务系列id 获取人物身上的任务
            if (task!=null) {//如果人物身上有这个任务
                TimeControlRunnable.scriptTask=scriptTask;//set 自动任务模式
                upTask(task, task.getTaskState(), isVip);//调用自动任务模式处理
            }else {//如果人物身上没有这个任务
                //生成脚本 前往领取任务
                RoleShow roleShow =ImageMixDeal.userimg.getRoleShow();
                List<Object> list = null;
                if (isVip) {
                    list = new ArrayList<>();
                    ScriptOpen openDoor=new ScriptOpen(101);
                    openDoor.setDoor(scriptTask.getMap());
                    openDoor.setX(scriptTask.getX());openDoor.setY(scriptTask.getY());
                    list.add(0,openDoor);
                }else {
                    list=XLPath.ZDXL(roleShow.getX(),roleShow.getY(),roleShow.getMapid().intValue(),scriptTask.getX(),scriptTask.getY(),scriptTask.getMap());
                }
                if (list==null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
                    return;
                }
                ScriptOpen open=new ScriptOpen(1);
                open.setNpc(scriptTask.getNpcID());
                list.add(0,open);

                ScriptOpen openTwo=new ScriptOpen(102);
                openTwo.setNpc(scriptTask.getTaskSetID());
                list.add(0,openTwo);

                TimeControlRunnable.scriptTask=scriptTask;
                TimeControlRunnable.addScript(list);
            }
        }
    }
    public static void upTask(Task task,int state,boolean is){
        synchronized (object) {
            if (is&&activeBases==null) {return;}
            //自动任务模式为空 或者 自动任务模式系列id 不同 退出
            if (scriptTask==null||scriptTask.getTaskSetID()!=task.getTaskData().getTaskSetID()) {return;}
            //状态是应超时取消任务  或者 取消任务   清空自动任务模式  退出
            if (state==TaskState.quxiaoTime||state==TaskState.quxiao) {scriptTask=null;return;}
            //如果你是队员  清空自动任务模式  退出
            if (ImageMixDeal.userimg.getTeams()==null) {scriptTask=null;return;}
            if (state==TaskState.finishTask) {//完成任务
                //回归接下一个任务  生成脚本
                List<Object> list=new ArrayList<>();
                ScriptOpen openDoor=new ScriptOpen(101);
                openDoor.setDoor(scriptTask.getMap());
                openDoor.setX(scriptTask.getX());openDoor.setY(scriptTask.getY());
                list.add(0,openDoor);

                ScriptEnd endDoor=new ScriptEnd(0,openDoor.getDoor(),openDoor.getX(),openDoor.getY());
                list.add(0,endDoor);
                list.add(null);list.add(null);//卡2个执行判断
                if (scriptTask.upSum()<0) {
                    scriptTask=null;
                    if (is) {
                        TimeControlRunnable.addScript(list);
                    } else {
                        TimeControlRunnable.addAllTask(null);
                    }
                    return;
                }
                ScriptOpen open=new ScriptOpen(1);
                open.setNpc(scriptTask.getNpcID());
                list.add(0,open);

                ScriptOpen openTwo=new ScriptOpen(102);
                openTwo.setNpc(scriptTask.getTaskSetID());
                list.add(0,openTwo);

                TimeControlRunnable.addScript(list);
            }else {//判断任务条件 生成脚本
                TaskProgress progress=task.getTaskProgress();
                if (progress!=null&&progress.getMap()!=0) {
                    RoleShow roleShow =ImageMixDeal.userimg.getRoleShow();
                    List<Object> list= null;
                    if (is) {
                        list = new ArrayList<>();
                        ScriptOpen openDoor=new ScriptOpen(101);
                        openDoor.setDoor(progress.getMap());
                        openDoor.setX(progress.getX());openDoor.setY(progress.getY());
                        list.add(0,openDoor);
                    } else {
                        list=XLPath.ZDXL(roleShow.getX(),roleShow.getY(),roleShow.getMapid().intValue(),progress.getX(),progress.getY(),progress.getMap());
                    }
                    if (list==null) {
                        ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
                        return;
                    }
                    ScriptOpen open=new ScriptOpen(progress.getType()==1?2:1);
                    open.setNpc(progress.getDId());
                    list.add(0, open);
                    if (progress.getType()==1||progress.getType()==2) {
                        //击杀NPC
                        ScriptOpen openTwo=new ScriptOpen(103);
                        openTwo.setNpc(progress.getDId());
                        list.add(0,openTwo);
                    }else if (progress.getType()==4) {
                        if (progress.getType()==4) {
                            //领取任务
                            if (!GoodsListFromServerUntil.isgood(progress.getDId(),1)){
                                removeScript(is);
                                ZhuFrame.getZhuJpanel().addPrompt2("你身上无任务物品，任务已结束");
                                return;
                            }
                            ScriptOpen openTwo=new ScriptOpen(106);
                            openTwo.setNpc(progress.getDId());
                            openTwo.setGoodsgid(progress.getGId());
                            list.add(0,openTwo);
                        }
                    }
                    TimeControlRunnable.addScript(list);
                }
            }
        }
    }
    public static void addTask(ScriptTask scriptTask){
        synchronized (object) {
            removeScript();//清理之前的脚本纪录
            TimeControlRunnable.scriptTask=null;//清空之前的自动任务模式
            Task task = Hero.getHero().getSetTask(scriptTask.getTaskSetID());//根据任务系列id 获取人物身上的任务
            if (task!=null) {//如果人物身上有这个任务
                TimeControlRunnable.scriptTask = scriptTask;//set 自动任务模式
                upTask(task, task.getTaskState());//调用自动任务模式处理
            }else {//如果人物身上没有这个任务
                //生成脚本 前往领取任务
                RoleShow roleShow = ImageMixDeal.userimg.getRoleShow();
                if (JirenjiaobenchuliControl.name != null)
                    roleShow = JirenjiaobenchuliControl.duizhang.getRoleShow();
                List<Object> list = XLPath.ZDXL(roleShow.getX(), roleShow.getY(), roleShow.getMapid().intValue(), scriptTask.getX(), scriptTask.getY(), scriptTask.getMap());
                if (list == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
                    return;
                }
                ScriptOpen open = new ScriptOpen(1);
                open.setNpc(scriptTask.getNpcID());
                list.add(0, open);

                ScriptOpen openTwo = new ScriptOpen(102);
                openTwo.setNpc(scriptTask.getTaskSetID());
                list.add(0, openTwo);

                TimeControlRunnable.scriptTask = scriptTask;
                TimeControlRunnable.addScript(list);
            }
        }
    }
    public static void xunlu(final int mapd,int x,int Y) {
        synchronized (TimeControlRunnable.object) {
            removeScript();
            TimeControlRunnable.scriptTask = null;

            {
                final RoleShow roleShow = ImageMixDeal.userimg.getRoleShow();
                final List<Object> list = XLPath.ZDXL(roleShow.getX(), roleShow.getY(), roleShow.getMapid().intValue(), x, Y, mapd);
                if (list == null) {
                    ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
                    return;
                }
                else {
                    final ScriptOpen open = new ScriptOpen(1);
                    // open.setNpc(scriptTask.getNpcID());
                    //  list.add(0, open);
                    final ScriptOpen openTwo = new ScriptOpen(102);
                    //openTwo.setNpc(scriptTask.getTaskSetID());
                    //   list.add(0, openTwo);
                    //  TimeControlRunnable.scriptTask = scriptTask;
                    addScript(list);
                }
            }
        }
    }
    public static void upTask(Task task,int state){
        synchronized (object) {
            //自动任务模式为空 或者 自动任务模式系列id 不同 退出
            if (scriptTask==null||scriptTask.getTaskSetID()!=task.getTaskData().getTaskSetID()) {return;}
            //状态是应超时取消任务  或者 取消任务   清空自动任务模式  退出
            if (state==TaskState.quxiaoTime||state==TaskState.quxiao) {scriptTask=null;return;}

            //如果你是队员  清空自动任务模式  退出
            if (JirenjiaobenchuliControl.name==null&&ImageMixDeal.userimg.getTeams()==null) {scriptTask=null;return;}
            if (state==TaskState.finishTask) {//完成任务
                //回归接下一个任务  生成脚本
                List<Object> list=new ArrayList<>();
                ScriptOpen openDoor=new ScriptOpen(101);
                openDoor.setDoor(scriptTask.getMap());
                openDoor.setX(scriptTask.getX());openDoor.setY(scriptTask.getY());
                list.add(0,openDoor);

                ScriptEnd endDoor=new ScriptEnd(0,openDoor.getDoor(),openDoor.getX(),openDoor.getY());
                list.add(0,endDoor);
                list.add(null);list.add(null);//卡2个执行判断
                if (scriptTask.upSum()<0) {
                    scriptTask=null;
                    TimeControlRunnable.addScript(list);
                    return;
                }
                ScriptOpen open=new ScriptOpen(1);
                open.setNpc(scriptTask.getNpcID());
                list.add(0,open);

                ScriptOpen openTwo=new ScriptOpen(102);
                openTwo.setNpc(scriptTask.getTaskSetID());
                list.add(0,openTwo);

                TimeControlRunnable.addScript(list);
            }else {//判断任务条件 生成脚本
                TaskProgress progress=task.getTaskProgress();
                if (progress!=null&&progress.getMap()!=0) {
                    RoleShow roleShow =ImageMixDeal.userimg.getRoleShow();
                    List<Object> list=XLPath.ZDXL(roleShow.getX(),roleShow.getY(),roleShow.getMapid().intValue(),progress.getX(),progress.getY(),progress.getMap());
                    if (list==null) {
                        ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
                        return;
                    }
                    ScriptOpen open=new ScriptOpen(progress.getType()==1?2:1);
                    open.setNpc(progress.getDId());
                    list.add(0, open);
                    if (progress.getType()==1||progress.getType()==2) {
                        //击杀NPC
                        ScriptOpen openTwo=new ScriptOpen(103);
                        openTwo.setNpc(progress.getDId());
                        list.add(0,openTwo);
                    }
                    TimeControlRunnable.addScript(list);
                }
            }
        }
    }
    //接取任务  寻路  执行任务 任务完成 传送回去  有剩余的任务执行次数 回到第一条
    /**添加脚本*/
    public static void addScript(List<Object> list){
        synchronized (object) {
            script=list;
            isScript=true;
        }
    }
    /**取消脚本*/
    public static void removeScript(){
        synchronized (object) {
            script=null;
            isScript=false;
        }
    }
    public static void removeScript(boolean is){
        synchronized (object) {
            script=null;
            isScript=false;
            count = 0;
            if (is) {
                activeBases=null;
                ZhuShouJframe.getZhuShouJframe().getZhuShouJpanel().getZhiXingBtn().setStart(false);
            }
        }
    }
}

//{
//    private int jg=0;
//    private int tx=0;
//    private int dk=0;
//    public static int JBTime=0;//防脚本
//    private static boolean isScript=false;
//    private static Object object=new Object();
//    private Integer count = 0;
//    private static Integer type = -1;
//    public static boolean b = true;
//    public TimeControlRunnable() {
//        // TODO Auto-generated constructor stub
//    }
//
//    @Override
//    public void run() {
//        // TODO Auto-generated method stub
////    	int dk = 0;
//        while (true) {
//            try {
//                long x=System.currentTimeMillis();
//                int cha=25;
//                if (FightingMixDeal.State==0){
//                    tx=6;
//                    cha=150;
//                }else {
//                    tx++;
//                    FightingMixDeal.PalyProgress(25);
//                }
//                if (tx>5) {
//                    tx=0;
//                    CommonUse();
//                }
//
//                x=System.currentTimeMillis()-x;
//                if (x<cha){
//                    Thread.sleep(cha-x);
//                }
//                Util.addtime(cha);
//            } catch (InterruptedException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
//    }
//    /**通用处理*/
//    public void CommonUse(){
//        jg++;
//        if (JBTime>0) {
//            JBTime-=150;
//            if (JBTime<=0) {
//                AntiPluginJframe.getAntiPluginJframe().getPluginJpanel().overtime();
//            }
//        }
//        // 画出组队申请
//        if (ZhuFrame.getZhuJpanel().getTeamState() == 1){
//            ZhuJpanel.getGroupBtn().imgchange((ZhuJpanel.getGroupBtn().getImgzhen() + 1) % 2);
//        }
//        // 画出好友消息friends
//        if (MessagrFlagUntil.NotReads.size() > 0){
//            ZhuJpanel.getFriendsBtn().imgchange((ZhuJpanel.getFriendsBtn().getImgzhen() + 1) % 2);
//        }
//        if (isScript&&jg%4==0) {
//            scriptHandle();
//        }
//        if (jg%30==0) {
//            TimeLimit.Timeout();
//        }else if (jg>500) {
//            jg=0;
//            // 判断宝宝状态
//            if (RoleData.getRoleData().getLoginResult().getHavebaby() > 0){
//                BabyUntil.calculateBaby();
//            }
//            Hero.getHero().OverTime();
//            PKSys.getPkSys().isexpiation();
//            SpriteFactory.ResetAndRemove();
//            CutButtonImage.removeNPCHeadImg();
//            for (int i = ImageMixDeal.npcimglist.size()-1; i >=0 ; i--) {
//                ImageMixDeal.npcimglist.get(i).npcmsg();
//            }
//            GoodsListFromServerUntil.isSendPackRecord();
//        }
//    }
//    /**脚本处理*/
//    public void scriptHandle() {
//        synchronized (object) {
//            if(!GameClient.channel.isActive())
//                return;
//
//            int size = script.size();
//            if (size == 0) {
//                isScript = false;
//                return;
//            }
//            size -= 1;
//            Object object = script.get(size);
//            if (object == null) {
//                script.remove(size);
//                return;
//            }
//            if (object instanceof ScriptOpen) {
//                //0寻路  1点击NPC  2点击野怪  3选择传送选项  4是点击自定义野怪
//                ScriptOpen open=(ScriptOpen) object;
//                if (open.getType()==0) {
//                    Mouselistener.XL(open.getX()/20, open.getY()/20);
//                }else if (open.getType()==1) {
//                    ManimgAttribute attribute=ImageMixDeal.getNpc(open.getNpc()+"");
//                    if (attribute!=null) {
//                        NpcMonitor.npc(attribute);
//                    }
//                }else if (open.getType()==2) {
//                    ManimgAttribute attribute=ImageMixDeal.getTask(open.getNpc());
//                    if (attribute!=null) {
//                        TaskMonitor.TaskCreeps(attribute.getTaskdata());
//                    }
//                }else if (open.getType()==3) {
//                    Door door=UserMessUntil.getDoor(open.getDoor()+"");
//                    if (door!=null) {
//                        TP.tp(door, 3);
//                    }
//                    FormsManagement.HideForm(27);
//                }else if (open.getType()==101) {//传送指定位置
//                    TP.tp(open.getDoor(),open.getX(),open.getY());
//                }else if (open.getType()==102) {//领取任务id
//                    TaskMixDeal.isoption(open.getNpc(),null,NPCJfram.getNpcJfram().getNpcjpanel().getNpcInfoBean().getNpctable().getNpcway());
//                    FormsManagement.HideForm(27);
//                }else if (open.getType()==103) {//攻击NPC 或者 任务怪
//                    MapAction.npcmenuAction.get(NpcMenuUntil.getMenu48()).menuControl("我是来击杀你的");
//                    FormsManagement.HideForm(27);
//                }
//                script.remove(size);
//            }else if (object instanceof ScriptEnd) {
//                ScriptEnd end=(ScriptEnd) object;
//                if (end.getType()==0) {
//                    RoleShow show=ImageMixDeal.userimg.getRoleShow();
//                    if (JirenjiaobenchuliControl.name!=null){
//                        if (ImageMixDeal.Playerimgmap.get(JirenjiaobenchuliControl.name)!=null)
//                            show=ImageMixDeal.Playerimgmap.get(JirenjiaobenchuliControl.name).getRoleShow();
//                    }
//                    // RoleShow show=ImageMixDeal.userimg.getRoleShow();
//                    if (show.getMapid().intValue()==end.getMap()&&Math.abs(show.getX()-end.getX())<200&&Math.abs(show.getY()-end.getY())<200) {
//                        script.remove(size);
//                    } else {
//                        count += 5;
////                            System.out.printf("55555555555555555555555555555555555");
//                    }
//                    if (count >= 500) {
//                        if (type == 1) {//新自动任务
//                            removeScript();
//                            Map<Integer, ActivityModelJpanel> mapActivityModelPanel = null;
//                            ActivityModelJpanel activityModelJpanel = null;
//                            try {
//                                mapActivityModelPanel = ActivityJframe.getActivityJframe().getActivityJpanel().getMapActivityModelPanel();
//                                activityModelJpanel = mapActivityModelPanel.get(scriptTask.getTaskSetID());
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//                            if (activityModelJpanel != null && type == 1) {
//                                String guide = activityModelJpanel.getActiveBase().getGuide();
//                                String[] v = guide.split("-");
//                                if (v.length == 5) {
//                                    TimeControlRunnable.addTask(new ScriptTask(v, activityModelJpanel.getActiveBase().getSid()));
//                                    count = 0;
//                                    return;
//                                }
//                            }
//                        } else if (type == 2) {
//                            removeScript();
//                            ZhuFrame.getZhuJpanel().getTaskGuideView().mouseReleased(null);
//                            count = 0;
//                        }
//                    }
//                }
//            }
//        }
//    }
//    private static List<Object> script;
//    public static ScriptTask scriptTask;//任务处理
//    public static void addTask(ScriptTask scriptTask){
//        synchronized (object) {
//            removeScript();//清理之前的脚本纪录
//            TimeControlRunnable.scriptTask=null;//清空之前的自动任务模式
//            Task task=Hero.getHero().getSetTask(scriptTask.getTaskSetID());//根据任务系列id 获取人物身上的任务
//            if (task!=null) {//如果人物身上有这个任务
//                TimeControlRunnable.scriptTask=scriptTask;//set 自动任务模式
//                upTask(task, task.getTaskState());//调用自动任务模式处理
//            }else {//如果人物身上没有这个任务
//                //生成脚本 前往领取任务
//                RoleShow roleShow =ImageMixDeal.userimg.getRoleShow();
//                if (JirenjiaobenchuliControl.duizhang!=null){
//                    roleShow=JirenjiaobenchuliControl.duizhang.getRoleShow();
//                }
//
//                List<Object> list=XLPath.ZDXL(roleShow.getX(),roleShow.getY(),roleShow.getMapid().intValue(),scriptTask.getX(),scriptTask.getY(),scriptTask.getMap());
//                if (list==null) {
//                    ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
//                    return;
//                }
//                ScriptOpen open=new ScriptOpen(1);
//                open.setNpc(scriptTask.getNpcID());
//                list.add(0,open);
//
//                ScriptOpen openTwo=new ScriptOpen(102);
//                openTwo.setNpc(scriptTask.getTaskSetID());
//                list.add(0,openTwo);
//
//                TimeControlRunnable.scriptTask=scriptTask;
//                TimeControlRunnable.addScript(list);
//            }
//        }
//    }
//    public static void upTask(Task task,int state){
//        synchronized (object) {
//            //自动任务模式为空 或者 自动任务模式系列id 不同 退出
//            if (scriptTask==null||scriptTask.getTaskSetID()!=task.getTaskData().getTaskSetID()) {return;}
//            //状态是应超时取消任务  或者 取消任务   清空自动任务模式  退出
//            if (state==TaskState.quxiaoTime||state==TaskState.quxiao) {scriptTask=null;return;}
//            //如果你是队员  清空自动任务模式  退出
//            if (JirenjiaobenchuliControl.name==null&&ImageMixDeal.userimg.getTeams()==null) {scriptTask=null;return;}
//            if (state==TaskState.finishTask) {//完成任务
//                //回归接下一个任务  生成脚本
//                List<Object> list=new ArrayList<>();
//                ScriptOpen openDoor=new ScriptOpen(101);
//                openDoor.setDoor(scriptTask.getMap());
//                openDoor.setX(scriptTask.getX());openDoor.setY(scriptTask.getY());
//                list.add(0,openDoor);
//
//                ScriptEnd endDoor=new ScriptEnd(0,openDoor.getDoor(),openDoor.getX(),openDoor.getY());
//                list.add(0,endDoor);
//                list.add(null);list.add(null);//卡2个执行判断
//                if (scriptTask.upSum()<0) {
//                    scriptTask=null;
//                    TimeControlRunnable.addScript(list);
//                    return;
//                }
//                ScriptOpen open=new ScriptOpen(1);
//                open.setNpc(scriptTask.getNpcID());
//                list.add(0,open);
//
//                ScriptOpen openTwo=new ScriptOpen(102);
//                openTwo.setNpc(scriptTask.getTaskSetID());
//                list.add(0,openTwo);
//
//                TimeControlRunnable.addScript(list);
//            }else {//判断任务条件 生成脚本
//                TaskProgress progress=task.getTaskProgress();
//                if (progress!=null&&progress.getMap()!=0) {
//                    RoleShow roleShow =ImageMixDeal.userimg.getRoleShow();
//                    List<Object> list=XLPath.ZDXL(roleShow.getX(),roleShow.getY(),roleShow.getMapid().intValue(),progress.getX(),progress.getY(),progress.getMap());
//                    if (list==null) {
//                        ZhuFrame.getZhuJpanel().addPrompt2("你所在位置无法达到目的地");
//                        return;
//                    }
//                    ScriptOpen open=new ScriptOpen(progress.getType()==1?2:1);
//                    open.setNpc(progress.getDId());
//                    list.add(0, open);
//                    if (progress.getType()==1||progress.getType()==2) {
//                        //击杀NPC
//                        ScriptOpen openTwo=new ScriptOpen(103);
//                        openTwo.setNpc(progress.getDId());
//                        list.add(0,openTwo);
//                    }
//                    TimeControlRunnable.addScript(list);
//                }
//            }
//        }
//    }
//    //接取任务  寻路  执行任务 任务完成 传送回去  有剩余的任务执行次数 回到第一条
//    /**添加脚本*/
//    public static void addScript(List<Object> list){
//        synchronized (object) {
//            script=list;
//            isScript=true;
//        }
//    }
//    /**
//     * 添加脚本
//     */
//    public static void addScript(List<Object> list, int type1) {
//        synchronized (object) {
//            script = list;
//            isScript = true;
//        }
//        type = type1;
//    }
//
//    /**取消脚本*/
//    public static void removeScript(){
//        synchronized (object) {
//            script=null;
//            isScript=false;
//        }
//    }
//}
